//module.exports = 
class DBTools {
  constructor(options) {
    options = options || {};
    this.database = null;
    this.DateBaseName = options.DateBaseName || 'LeaderInfoDB';
    this.Version = options.Version || '1.0';
    this.Description = options.Description || 'LeaderInfoSystem';
    this.DataBaseSize = options.DataBaseSize || 2 * 1024 * 1024;
    this.init();
  }
  /**
   * 初始化数据库
   */
  init() {
    this.database = openDatabase(
      this.DateBaseName,
      this.Version,
      this.Description,
      this.DataBaseSize
    );
  }
  /**
   * 批量添加字段
   * @param {*} tableName 表名
   * @param {*} arr 更新的数据    [{key1：value1 , key2 : value2 ...},{key1：value1 , key2 : value2 ...}]
   * @param {*} index BLOG字段所在的索引位置
   * @param {*} isFirst 是否是第一次创建表
   */
  addBlob(tableName, arr, index, isFirst = true) {
    var _db = this.database;
    var _me = this;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      if (arr == null) {
        return this;
      }
      var keyC = [];
      var keyI = [];
      var _key = '';
      arr = arr || [];
      if (arr && arr.constructor == Array) {
        for (var i in arr[0]) {
          keyC.push(i);
          keyI.push(i);
        }
        keyC[0] = keyC[0] + ' unique';
        _key = keyI.join(',');
        index = index == undefined ? 0 : index;
        keyC[index] = keyC[index] + ' BLOB';
        // eslint-disable-next-line promise/param-names

        _db.transaction(function (tx, result) {
          // var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')';
          // console.log('csql:' + csql);
          if (isFirst == true) {
            tx.executeSql(
              'CREATE TABLE IF NOT EXISTS ' +
              tableName +
              ' (' +
              keyC.join(',') +
              ')'
            );
          }
          // var sql = "";
          for (var s = 0, _len = arr.length; s < _len; s++) {
            var _value = _me.split(arr[s]);
            // sql += 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')';
            // console.log("sql:" + sql);
            tx.executeSql(
              'INSERT INTO ' +
              tableName +
              ' (' +
              _key +
              ') VALUES (' +
              _value +
              ')',
              [],
              function (tx, result) {
                resovle(result.rowsAffected);
                console.log('添加成功' + result.rowsAffected);
              },
              function (tx) {
                console.error('添加失败');
                // eslint-disable-next-line prefer-promise-reject-errors
                reject(false);
              }
            );
          }
          _key = keyI = keyC = null;
          resovle(arr.length);
        });
      }
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 批量添加字段
   * @param {*} tableName 表名
   * @param {*} arr 更新的数据    [{key1：value1 , key2 : value2 ...},{key1：value1 , key2 : value2 ...}]
   * @param {*} index BLOG字段所在的索引位置
   * @param {*} firstKey firstKey 第一个字段是否是主键（默认是）
   * @param {*} isFirst 是否是第一次创建表
   */
  patchAddBlob(tableName, arr, index, firstKey = true, isFirst = true) {
    var _db = this.database;
    var _me = this;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      if (arr == null) {
        return this;
      }
      var keyC = [];
      var keyI = [];
      var _key = '';
      arr = arr || [];
      if (arr && arr.constructor == Array) {
        for (var i in arr[0]) {
          keyC.push(i);
          keyI.push(i);
        }
        if (firstKey) {
          keyC[0] = keyC[0] + ' unique';
        }
        _key = keyI.join(',');
        index = index == undefined ? 0 : index;
        keyC[index] = keyC[index] + ' text';
        // eslint-disable-next-line promise/param-names

        _db.transaction(function (tx, result) {
          // var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')';
          // console.log('csql:' + csql);
          if (isFirst == true) {
            tx.executeSql(
              'CREATE TABLE IF NOT EXISTS ' +
              tableName +
              ' (' +
              keyC.join(',') +
              ')'
            );
          }
          var sql = '';
          var _values = [];
          for (var s = 0, _len = arr.length; s < _len; s++) {
            _values.push('(' + _me.split(arr[s]) + ')');
          }
          sql =
            'INSERT INTO ' +
            tableName +
            ' (' +
            _key +
            ') VALUES ' +
            _values.join(',');
          // console.log('sql:' + sql);
          tx.executeSql(
            sql,
            [],
            function (tx, result) {
              resovle(result.rowsAffected);
              // console.log('添加成功'+result.rowsAffected);
            },
            function (tx, error) {
              console.error('添加失败', tx, error);
              // eslint-disable-next-line prefer-promise-reject-errors
              reject(false);
            }
          );
          _key = keyI = keyC = null;
          resovle(arr.length);
        });
      }
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 批量添加字段 注 ： 数据里面的第一个key 为主键
   * @param {*} tableName 表名
   * @param {*} arr arr 更新的数据    [{key1：value1 , key2 : value2 ...},{key1：value1 , key2 : value2 ...}]
   * @param {*} firstKey firstKey 第一个字段是否是主键（默认是）
   */
  add(tableName, arr, firstKey = true) {
    var _me = this;
    var _db = this.database;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      if (arr == null) {
        return this;
      }
      var keyC = [];
      var keyI = [];
      var _key = '';
      arr = arr || [];
      if (arr && arr.constructor == Array) {
        for (var i in arr[0]) {
          keyC.push(i);
          keyI.push(i);
        }
        if (firstKey) {
          keyC[0] = keyC[0] + ' unique';
        }
        _key = keyI.join(',');
        _db.transaction(function (tx) {
          // /var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')';
          // console.log('csql:' + csql);
          tx.executeSql(
            'CREATE TABLE IF NOT EXISTS ' +
            tableName +
            ' (' +
            keyC.join(',') +
            ')'
          );
          // var sql = "";
          for (var s = 0, _len = arr.length; s < _len; s++) {
            var _value = _me.split(arr[s]);
            // sql += 'INSERT INTO ' + tableName + ' (' + _key + ') VALUES (' + _value + ')';
            // console.log("sql:" + sql);
            tx.executeSql(
              'INSERT INTO ' +
              tableName +
              ' (' +
              _key +
              ') VALUES (' +
              _value +
              ')',
              [],
              function (tx, result) {
                resovle(result.rowsAffected);
                // console.log('添加成功'+result.rowsAffected);
              },
              function (tx, error) {
                console.error('添加失败', error);
                // eslint-disable-next-line prefer-promise-reject-errors
                reject(false);
              }
            );
          }
          _key = keyI = keyC = null;
          resovle(arr.length);
        });
      }
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 批量添加行记录 注 ： 数据里面的第一个key 为主键
   * @param {*} tableName 表名
   * @param {*} arr arr 更新的数据    [{key1：value1 , key2 : value2 ...},{key1：value1 , key2 : value2 ...}]
   * @param {*} firstKey firstKey 第一个字段是否是主键（默认是）
   */
  patchAdd(tableName, arr, firstKey = true) {
    var _me = this;
    var _db = this.database;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      if (arr == null) {
        return this;
      }
      var keyC = [];
      var keyI = [];
      var _key = '';
      arr = arr || [];
      if (arr && arr.constructor == Array) {
        for (var i in arr[0]) {
          keyC.push(i);
          keyI.push(i);
        }
        if (firstKey) {
          keyC[0] = keyC[0] + ' unique';
        }
        _key = keyI.join(',');
        _db.transaction(function (tx) {
          // /var csql = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + keyC.join(",") + ')';
          // console.log('csql:' + csql);
          tx.executeSql(
            'CREATE TABLE IF NOT EXISTS ' +
            tableName +
            ' (' +
            keyC.join(',') +
            ')'
          );
          var sql = '';
          var _values = [];
          for (var s = 0, _len = arr.length; s < _len; s++) {
            _values.push('(' + _me.split(arr[s]) + ')');
          }
          sql =
            'INSERT INTO ' +
            tableName +
            ' (' +
            _key +
            ') VALUES ' +
            _values.join(',');
          console.log('sql:' + sql);

          tx.executeSql(
            sql,
            [],
            function (tx, result) {
              resovle(result.rowsAffected);
              // console.log('添加成功'+result.rowsAffected);
            },
            function (tx, error) {
              console.error('添加失败');
              // eslint-disable-next-line prefer-promise-reject-errors
              reject(false);
            }
          );
          _key = keyI = keyC = null;
          resovle(arr.length);
        });
      }
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 更新指定数据
   * @param {*} tableName 表名
   * @param {*} key 查询的键
   * @param {*} value 对应键的值
   * @param {*} obj obj 更新的数据    {key1：value1 , key2 : value2 ...}
   */
  update(tableName, key, value, obj) {
    var _db = this.database;
    var _value = this.splitU(obj);
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      _db.transaction(function (tx) {
        // console.log('sql:' + 'UPDATE ' + tableName + ' set ' + _value + ' where ' + key + '="' + value + '"')
        tx.executeSql(
          'UPDATE ' +
          tableName +
          ' set ' +
          _value +
          ' where ' +
          key +
          '="' +
          value +
          '"',
          [],
          function (tx, result) {
            resovle(result.rowsAffected);
          },
          function (tx, error) {
            console.error('更新失败');
            // eslint-disable-next-line prefer-promise-reject-errors
            reject(false);
          }
        );
      });
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 更新指定数据
   * @param {*} tableName 表名
   * @param {*} condition 查询条件
   * @param {*} obj obj 更新的数据    {key1：value1 , key2 : value2 ...}
   */
  updateWhere(tableName, condition, obj) {
    var _db = this.database;
    var _value = this.splitU(obj);
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      _db.transaction(function (tx) {
        console.log('UPDATE ' + tableName + ' set ' + _value + ' ' + condition);
        tx.executeSql(
          'UPDATE ' + tableName + ' set ' + _value + +' ' + condition,
          [],
          function (tx, result) {
            resovle(result.rowsAffected);
          },
          function (tx, error) {
            console.error('更新失败', error);
            // eslint-disable-next-line prefer-promise-reject-errors
            reject(false);
          }
        );
      });
    }).catch(error => {
      console.log('error :', error);
    });
  }
  /**
   * 读取表数据
   * @param {*} tableName 表名
   * @param {*} condition 查询条件   'where name="jiekzou"'
   */
  read(tableName, condition) {
    var _db = this.database;
    var _me = this;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      var _condition = _me.isString(condition) ? condition : '';
      let sqlString = 'SELECT * FROM ' + tableName + ' ' + _condition + ' ';

      var _re = [];
      _db.transaction(function (tx) {
        tx.executeSql(
          'SELECT * FROM ' + tableName + ' ' + _condition + ' ',
          [],
          function (tx, results) {
            if (results && results.rows) {
              _re = _me.toArray(results.rows);
              //console.log(_re)
              resovle(_re);
            } else {
              resovle([]);
            }
          },
          function (tx, error) {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject([JSON.stringify(error), sqlString]);
          }
        );
      });
    }).catch(error => {
      console.log('error :', error);
      reject(error);
    });
  }
  /**
   * 读取表数据
   * @param {*} tableName 表名
   * @param {*} field 查询字段，逗号隔开
   * @param {*} condition 查询条件   'where name="jiekzou"'
   * resovle 要返回的值是一个数组,格式是[err,data]
   */
  readField(tableName, field = '*', condition) {
    var _db = this.database;
    var _me = this;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      var _condition = _me.isString(condition) ? condition : '';
      var _re = [];
      let sqlString = 'SELECT ' + field + ' FROM ' + tableName + ' ' + _condition + ' ';
      console.log(sqlString)
      _db.transaction(function (tx) {
        tx.executeSql(
          'SELECT ' + field + ' FROM ' + tableName + ' ' + _condition + ' ',
          [],
          function (tx, results) {
            if (results && results.rows) {
              _re = _me.toArray(results.rows);
              resovle(_re);
            } else {
              resovle([]);
            }
          },
          function (tx, error) {
            // eslint-disable-next-line prefer-promise-reject-errors
            // console.log('executeSql failed: '+JSON.stringify(error)); 
            // console.log('executeSql failed: '+sqlString); 
            reject([JSON.stringify(error), sqlString]);

          }
        );
      });
    }).catch(error => {
      console.log('error :', error);
      //reject(error);
    });
  }



  /**
   * 读取表数据
   * @param {*} tableName 表名
   * @param {*} field 查询字段，逗号隔开
   * @param {*} condition 查询条件   'where name="jiekzou"'
   * resovle 要返回的值是一个数组,格式是[err,data]
   */
  readFieldByCommand(sqlStringCommand) {
    var _db = this.database;
    var _me = this;
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
     
      var _re = [];
      let sqlString = sqlStringCommand;
      //console.log(sqlString)
      _db.transaction(function (tx) {
        tx.executeSql(
          sqlString,
          [],
          function (tx, results) {
            if (results && results.rows) {
              _re = _me.toArray(results.rows);
              resovle(_re);
            } else {
              resovle([]);
            }
          },
          function (tx, error) {
            // eslint-disable-next-line prefer-promise-reject-errors
            console.log(error);
            // console.log('executeSql failed: '+sqlString);
            reject([JSON.stringify(error), sqlString]);

          }
        );
      });
    }).catch(error => {
      console.error('error :', error);
      //reject(error);
    });
  }


  /**
   * 删除数据
   * @param {*} tableName 表名
   * @param {*} condition 查询条件   'where name="jiekzou"'
   */
  remove(tableName, condition) {
    var _me = this;
    var _condition = _me.isString(condition) ? condition : '';
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      _me.database.transaction(function (tx) {
        tx.executeSql(
          'DELETE FROM ' + tableName + ' ' + _condition + ' ',
          [],
          function (tx, result) {
            resovle(result.rowsAffected);
          },
          function (tx, error) {
            // eslint-disable-next-line prefer-promise-reject-errors
            reject(false);
            console.error('删除失败');
          }
        );
      });
    }).catch(error => {
      console.log('error :', error);
    });
  }
  // /**
  //  * 根据查询条件读取表记录数
  //  * @param {*} tableName 表名
  //  * @param {*} condition 查询条件   'where name="jiekzou"'
  //  */
  // counts(tableName, condition) {

  //   return this.androidCounts(tableName, condition);

  // }

  // /**
  //  * 读取表数据（Android）
  //  * @param {*} tableName 表名
  //  * @param {*} condition 查询条件   'where name="jiekzou"'
  //  * 如果报错则返回的是 undefined
  //  */
  // androidCounts(tableName, condition) {
  //   var _db = this.database;
  //   var _me = this;
  //   var _condition = _me.isString(condition) ? condition : '';
  //   // eslint-disable-next-line promise/param-names
  //   return new Promise(function (resovle, reject) {
  //     var _re = [];
  //     let sqlString = 'SELECT count (*) as num FROM ' + tableName + ' ' + _condition + ' ';
  //     //console.log(sqlString)
  //     _db.transaction(function (tx) {
  //       tx.executeSql(
  //         sqlString,
  //         [],
  //         function (tx, results) {
  //           // count (*) as num
  //           if (results && results.rows) {
  //             if (results.rows[0]) {
  //               resovle(results.rows[0].num);
  //             } else {
  //               resovle(0);
  //             }
  //           } else {
  //             resovle(0);
  //           }
  //         },
  //         function (tx, error) {
  //           // eslint-disable-next-line prefer-promise-reject-errors
  //           reject(0);
  //           console.error('查询失败 '+sqlString);
  //         }
  //       );
  //     });
  //   }).catch(e => {
  //     // console.log('e :', e);
  //     // eslint-disable-next-line no-undef
  //     // reject(0);
  //   });
  // }
  /**
   * 删除数据表
   * @param {*} tableName 表名
   */
  delTable(tableName) {
    var _db = this.database;
    console.log('_db :', _db);
    // eslint-disable-next-line promise/param-names
    return new Promise(function (resovle, reject) {
      _db.transaction(function (tx) {
        tx.executeSql(
          'DROP TABLE IF EXISTS ' + tableName,
          [],
          function (tx, res) {
            resovle(1);
          },
          function (tx, err) {
            console.error(err);
            // eslint-disable-next-line prefer-promise-reject-errors
            reject(0);
          }
        );
      });
    });
  }
  // 更新字符处理
  splitU(obj) {
    var _arr = [];
    for (var t in obj) {
      _arr.push(t + '=\'' + obj[t] + '\'');
    }
    return _arr.join(',');
  }
  // 添加字符处理
  split(obj) {
    var _arr = [];
    for (var m in obj) {
      _arr.push('\'' + obj[m] + '\'');
    }
    return _arr.join(',');
  }
  isFunction(callback) {
    return !!(
      typeof callback != 'undefined' && callback.constructor == Function
    );
  }
  isString(string) {
    return typeof string == 'string';
  }
  toArray(obj) {
    var _arr = [];
    var _len = obj.length;
    if (_len > 0) {
      for (var i = 0; i < _len; i++) {

        _arr.push([null, obj.item(i)]);
      }
    }
    return _arr;
  }
}

export default DBTools;